package com.kede.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.kede.app.domain.ZhiGoodValidFail;
import com.kede.app.repository.ZhiGoodValidFailRepository;
import com.kede.app.service.ZhiGoodValidFailService;
import com.kede.app.service.ZhiUserCertifiedService;
import com.kede.app.service.dto.ZhiGoodValidFailDto;
import com.kede.app.service.dto.ZhiGoodValidFailQueryCriteria;
import com.kede.app.service.dto.ZhiUserCertifiedDto;
import com.kede.app.service.mapstruct.ZhiGoodValidFailMapper;
import com.kede.utils.FileUtil;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import com.kede.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-10-14
**/
@Service
@RequiredArgsConstructor
public class ZhiGoodValidFailServiceImpl implements ZhiGoodValidFailService {

    private final ZhiGoodValidFailRepository zhiGoodValidFailRepository;
    private final ZhiGoodValidFailMapper zhiGoodValidFailMapper;
    private final ZhiUserCertifiedService zhiUserCertifiedService;

    @Override
    public Map<String,Object> queryAll(ZhiGoodValidFailQueryCriteria criteria, Pageable pageable){
        Page<ZhiGoodValidFail> page = zhiGoodValidFailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        page.getContent().forEach(zhiGoodValidFail -> {
            ZhiUserCertifiedDto store = zhiUserCertifiedService.findById(zhiGoodValidFail.getStoreId());
            zhiGoodValidFail.setStoreName(store.getStoreName());
            if(ObjectUtil.isNotNull(zhiGoodValidFail.getQuId()) && !zhiGoodValidFail.getQuId().equals(0)){
                ZhiUserCertifiedDto qu = zhiUserCertifiedService.findById(zhiGoodValidFail.getQuId());
                zhiGoodValidFail.setQuName(qu.getStoreName());
            }
            if(ObjectUtil.isNotNull(zhiGoodValidFail.getShiId()) && !zhiGoodValidFail.getShiId().equals(0)){
                ZhiUserCertifiedDto shi = zhiUserCertifiedService.findById(zhiGoodValidFail.getShiId());
                zhiGoodValidFail.setShiName(shi.getStoreName());
            }
        });
        return PageUtil.toPage(page.map(zhiGoodValidFailMapper::toDto));
    }

    @Override
    public List<ZhiGoodValidFailDto> queryAll(ZhiGoodValidFailQueryCriteria criteria){
        return zhiGoodValidFailMapper.toDto(zhiGoodValidFailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhiGoodValidFailDto findById(Integer id) {
        ZhiGoodValidFail zhiGoodValidFail = zhiGoodValidFailRepository.findById(id).orElseGet(ZhiGoodValidFail::new);
        ValidationUtil.isNull(zhiGoodValidFail.getId(),"ZhiGoodValidFail","id",id);
        return zhiGoodValidFailMapper.toDto(zhiGoodValidFail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhiGoodValidFailDto create(ZhiGoodValidFail resources) {
        return zhiGoodValidFailMapper.toDto(zhiGoodValidFailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhiGoodValidFail resources) {
        ZhiGoodValidFail zhiGoodValidFail = zhiGoodValidFailRepository.findById(resources.getId()).orElseGet(ZhiGoodValidFail::new);
        ValidationUtil.isNull( zhiGoodValidFail.getId(),"ZhiGoodValidFail","id",resources.getId());
        zhiGoodValidFail.copy(resources);
        zhiGoodValidFailRepository.save(zhiGoodValidFail);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            zhiGoodValidFailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhiGoodValidFailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhiGoodValidFailDto zhiGoodValidFail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("物品id 关联 zhi_order_goods_list中的id", zhiGoodValidFail.getGoodId());
            map.put("门店id", zhiGoodValidFail.getStoreId());
            map.put("物品分类名称", zhiGoodValidFail.getCateName());
            map.put("置换商品图片", zhiGoodValidFail.getImages());
            map.put("置换商品信息", zhiGoodValidFail.getMessage());
            map.put("置换状态1 待确认 2确认失败", zhiGoodValidFail.getStatus());
            map.put("失败原因", zhiGoodValidFail.getReason());
            map.put("区代理id", zhiGoodValidFail.getQuId());
            map.put("是代理id", zhiGoodValidFail.getShiId());
            map.put("默认门店价格元", zhiGoodValidFail.getDefaultStoreMoney());
            map.put("默认区代价格元", zhiGoodValidFail.getDefaultQuMoney());
            map.put("默认市代价格元", zhiGoodValidFail.getDefaultShiMoney());
            map.put("确认置换时间", zhiGoodValidFail.getCreateTime());
            map.put("验收时间", zhiGoodValidFail.getUpdateTime());
            map.put("运输单id", zhiGoodValidFail.getTransportId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}